import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin' ;
import CleanWebpackPlugin from 'clean-webpack-plugin';

import webpack from 'webpack';

import path from 'path';

const rootPath = path.join(__dirname, './')

export default {
    devtool: '#source-map',

    entry: {
        index: ['babel-polyfill', './client/index.js'],
        vendor: ['dva-react-router-3']
    },

    output: {
        filename: '[name].[hash].js',
        path: path.join(__dirname, './dist'),
        publicPath: '/',
        chunkFilename: '[name].[chunkhash:10].js'
    },
    resolve: {
        extensions: [".js", ".jsx", "css", "less", "png", "jpg"],
        modules: [path.resolve(rootPath, "src"), "node_modules"],
    },
    module: {
        loaders: [ //TODO css 既要有style 又要有生成css文件
            {
                test: /\.html/,
                exclude: /node_modules/,
                loader: 'html-loader',
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
            }, /*{
                test: /\.(less|css)$/,
                exclude: /node_modules/,
                loader: ExtractTextPlugin.extract('css-loader?modules&localIdentName=[local]___[hash:base64:5]!less-loader'),
            }, */{
                test: /\.(less|css)$/,
                exclude: /node_modules/,
                loader: 'style-loader!css-loader?modules&localIdentName=[local]___[hash:base64:5]!less-loader',
            }, {
                test: /\.css$/,
                include: /node_modules/,
                loader: 'css-loader?sourceMap',
            }, {
                test: /\.(svg|woff2?|ttf|eot|jpe?g|png|gif)(\?.*)?$/i,
                exclude: /node_modules/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 1024,
                        name: 'img/[sha512:hash:base64:7].[ext]'
                    }
                }
            }
        ]
    },

    plugins: [
        // new ExtractTextPlugin({filename: '[name].css', disable: false, allChunks: true}),
        new webpack.optimize.CommonsChunkPlugin({
            name: ['vendor'],
            minChunks: 2
        }),
        new CleanWebpackPlugin('dist'),

        new HtmlWebpackPlugin({
            filename: './view/index.html',
            template: './template/index.html',
            chunks: {
                "head": {
                    "entry": "[name].[hash].js",
                },
            }
        }),
    ],
};
